package IOBaseTest;

import java.io.*;

/**
 * Created by yexuan on 2018/3/13.
 * 利用缓冲区拷贝图片
 */
public class Copy_PicDemo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        copy_pic_method();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }

    public static void copy_pic_method(){
        BufferedInputStream bfis = null;
        BufferedOutputStream bfos = null;
        try {
            bfis = new BufferedInputStream(new FileInputStream("C:\\Users\\dell-\\Desktop\\1.jpg"));
            bfos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\dell-\\Desktop\\2.jpg"));

            int by = 0;
            while ((by = bfis.read())!=-1){
                bfos.write(by);
            }

        }catch (IOException e) {
            throw new RuntimeException("复制出错");
        }
        finally {
            if(bfis!=null)
                try {
                   bfis.close();
                }catch (IOException e) {
                    throw new RuntimeException("关闭读流出错");
                }
            if(bfos!=null)
                try {
                    bfos.close();
                }catch (IOException e) {
                    throw new RuntimeException("关闭写流出错");
                }
        }
    }
}
